package com.ccys.cms.init;

import com.alibaba.fastjson.JSONObject;
import com.ccys.cms.service.ModuleService;
import com.ccys.common.constants.ContextConstants;
import com.ccys.common.constants.QSwaggerContext;
import com.ccys.common.constants.TouristUtil;
import com.ccys.common.enums.CodeEnum;
import com.ccys.common.third.push.ios.IosNotificationUtil;
import com.ccys.common.util.JedisUtil;
import com.ccys.common.util.Kit;
import com.ccys.common.util.SerializationUtil;
import com.ccys.db.cache.TranslationUtil;
import com.ccys.db.entity.TblAccount;
import com.ccys.db.entity.TblTranslation;
import com.ccys.db.mapper.TblAccountMapper;
import com.ccys.db.mapper.TblTranslationMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import redis.clients.jedis.Jedis;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

/**
 * 启动初始化
 */
@Configuration
@Slf4j
public class AdminInit implements CommandLineRunner {

    @Resource
    private ModuleService moduleService;
    @Resource
    private TblAccountMapper tblAccountMapper;

    @Resource
    private TblTranslationMapper translationMapper;

    @Override
    @Order(value = 1)
    public void run(String... args) throws Exception {
        //初始化模块
        moduleService.refreshCache();

        //加载游客模式
        initTouristApi();

        //swagger上下文加载
        setContext();

        //初始化开发者
        initUser();

        //初始化翻译
        initTranslation();


    }

    private void initTouristApi(){
        Map tourist = TouristUtil.tourist();
        Set set = tourist.keySet();
        for (Object key:set
        ) {
            log.info(String.valueOf(key));
            log.info(String.valueOf(tourist.get(key)));
        }
    }

    private void setContext() throws ClassNotFoundException {
        log.info("============正在加载必要上下文============");
        QSwaggerContext.setContext();
        Map<String, List<String>> contentMap = QSwaggerContext.CONTENT_MAP;
        for (String key :
                contentMap.keySet()) {
            log.info(key);
            log.info(contentMap.get(key).toString());
        }
    }

       /**
        * @description 描述
        * @date     2023/12/11
        * @author   cloud fly
        */
    private void initUser(){
        Jedis jedis = JedisUtil.getJedis();
        jedis.select(CodeEnum.REDIS_USER.codeOf());
        //log.info("--------------初始化后端管理用户缓存---------------");
        Example example = new Example(TblAccount.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("userStatus", 1);
        c.andEqualTo("userLoginStatus", 1);
        c.andCondition("user_type in " + Kit.getWhereByIds("dev"));
        List<TblAccount> userList = tblAccountMapper.selectByExample(example);
        if(userList.size()>0){
            for(TblAccount user:userList){
                JedisUtil.setexObj("dev", ContextConstants.DEFAULT_NOT_OPERATED_TIME, JSONObject.toJSON(user), CodeEnum.REDIS_USER.codeOf());
                JedisUtil.setex(user.getId(),ContextConstants.DEFAULT_NOT_OPERATED_TIME, "dev", CodeEnum.REDIS_USER.codeOf());
            }
        }
        //log.info("===========初始化管理用户缓存完成,总数:"+userList.size()+"=========");
    }

    /**
     * 初始化翻译内容
     */
    private void initTranslation(){
        List<TblTranslation> list = translationMapper.selectAll();
        list.forEach(t->{
            JedisUtil.set(TranslationUtil.PREFIX+t.getKeyWord()+":"+t.getLanguageId(),t.getContent(),CodeEnum.REDIS_TRANSLATION.codeOf());
        });
        log.info("初始化翻译内容成功,总条数:{}",list.size());
    }

}
